Fold expressions, introduced in C++17, are a way to expand a variadic template parameter pack with operators between each pack element. Due to the
high flexibility of this construct, many variadic templates that used to be written by a recursive call can now be written in a more direct way.
In addition to a usually simpler code, fold expressions results in far less functions instantiated, which can improve compilation time.
This rule raises an issue when a recursive template instantiation that could be easily be replaced by a fold expression is detected
Noncompliant code example
template<class Cont>
void addElementsToContainer(Cont &C) {
}
template<class Cont, class T, class ...U>
void addElementsToContainer(Cont &C, T &&t, U &&...us) {
C.push_back(forward<T>(t));
addElementsToContainer(C, forward<U>(us)...); // Noncompliant recursive call
}
Compliant solution
template<class Cont, class ...T>
void addElementsToContainer(Cont &C, T &&...ts) {
(C.push_back(std::forward<T>(ts)),...); // Compliant fold expression over the operator ','
}